﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace BST250301TestStand.Views
{
    /// <summary>
    /// 一个自定义的温度计 WinForms 控件
    /// </summary>
    public partial class ThermometerControl : Control
    {
        #region --- 属性 ---

        private float _value = 25.0f;
        [Category("Appearance"), Description("当前显示的温度值")]
        public float Value
        {
            get => _value;
            set
            {
                _value = Math.Max(_minimum, Math.Min(_maximum, value));
                this.Invalidate(); // 值改变时，重绘控件
            }
        }

        private float _minimum = 0.0f;
        [Category("Appearance"), Description("温度计的最小值")]
        public float Minimum
        {
            get => _minimum;
            set
            {
                _minimum = value;
                this.Invalidate();
            }
        }

        private float _maximum = 100.0f;
        [Category("Appearance"), Description("温度计的最大值")]
        public float Maximum
        {
            get => _maximum;
            set
            {
                _maximum = value;
                this.Invalidate();
            }
        }

        private Color _mercuryColor = Color.Red;
        [Category("Appearance"), Description("温度计内部液体的颜色")]
        public Color MercuryColor
        {
            get => _mercuryColor;
            set
            {
                _mercuryColor = value;
                this.Invalidate();
            }
        }

        private int _majorTickFrequency = 10;
        [Category("Appearance"), Description("主刻度线的频率")]
        public int MajorTickFrequency
        {
            get => _majorTickFrequency;
            set
            {
                _majorTickFrequency = value;
                this.Invalidate();
            }
        }

        private Color _tickColor = Color.Black;
        [Category("Appearance"), Description("刻度线的颜色")]
        public Color TickColor
        {
            get => _tickColor;
            set
            {
                _tickColor = value;
                this.Invalidate();
            }
        }

        #endregion

        public ThermometerControl()
        {
            // 设置默认大小
            this.Size = new Size(80, 250);

            // 启用双缓冲，防止绘图时闪烁
            this.SetStyle(ControlStyles.UserPaint |
                          ControlStyles.AllPaintingInWmPaint |
                          ControlStyles.OptimizedDoubleBuffer |
                          ControlStyles.ResizeRedraw, true);
        }

        /// <summary>
        /// 控件的核心绘制逻辑
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;
            // 使用抗锯齿模式，使图形更平滑
            g.SmoothingMode = SmoothingMode.AntiAlias;

            // 定义绘图区域，留出一些边距
            Rectangle drawingArea = new Rectangle(
                this.Padding.Left,
                this.Padding.Top,
                this.Width - this.Padding.Left - this.Padding.Right,
                this.Height - this.Padding.Top - this.Padding.Bottom);

            // 1. 计算几何尺寸
            float bulbDiameter = Math.Min(drawingArea.Width, drawingArea.Height) * 0.3f;
            float tubeWidth = bulbDiameter * 0.5f;

            RectangleF bulbRect = new RectangleF(
                drawingArea.Left + (drawingArea.Width - bulbDiameter) / 2,
                drawingArea.Bottom - bulbDiameter,
                bulbDiameter,
                bulbDiameter);

            RectangleF tubeRect = new RectangleF(
                drawingArea.Left + (drawingArea.Width - tubeWidth) / 2,
                drawingArea.Top,
                tubeWidth,
                drawingArea.Height - bulbDiameter / 2);

            // 2. 绘制温度计外壳（玻璃）
            using (var glassBrush = new SolidBrush(Color.FromArgb(220, 220, 220))) // 半透明灰色
            using (var borderPen = new Pen(Color.Gray, 2))
            {
                g.FillEllipse(glassBrush, bulbRect);
                g.FillRectangle(glassBrush, tubeRect.X, tubeRect.Y, tubeRect.Width, tubeRect.Height - bulbDiameter / 2);
                g.DrawEllipse(borderPen, bulbRect);
                g.DrawLine(borderPen, tubeRect.Left, tubeRect.Top, tubeRect.Left, tubeRect.Bottom - bulbDiameter / 2);
                g.DrawLine(borderPen, tubeRect.Right, tubeRect.Top, tubeRect.Right, tubeRect.Bottom - bulbDiameter / 2);
            }

            // 3. 绘制“水银”液体
            if (Value > Minimum)
            {
                using (var mercuryBrush = new SolidBrush(this.MercuryColor))
                {
                    // 填充球体
                    g.FillEllipse(mercuryBrush, bulbRect);

                    // 计算液体柱的高度
                    float totalRange = _maximum - _minimum;
                    if (totalRange <= 0) return;

                    float valueRatio = (_value - _minimum) / totalRange;
                    float tubePixelHeight = tubeRect.Height - bulbDiameter; // 可填充的像素高度
                    float mercuryHeight = valueRatio * tubePixelHeight;

                    // 填充液体柱
                    RectangleF mercuryRect = new RectangleF(
                        tubeRect.X,
                        tubeRect.Bottom - bulbDiameter / 2 - mercuryHeight,
                        tubeRect.Width,
                        mercuryHeight);

                    g.FillRectangle(mercuryBrush, mercuryRect);
                }
            }

            // 4. 绘制刻度线和标签
            using (var tickPen = new Pen(this.TickColor, 1))
            using (var textBrush = new SolidBrush(this.ForeColor))
            using (var textFont = new Font(this.Font.FontFamily, 8f))
            {
                float tubePixelHeight = tubeRect.Height - bulbDiameter;
                float tubeBottomY = tubeRect.Bottom - bulbDiameter / 2;

                for (float i = _minimum; i <= _maximum; i += _majorTickFrequency)
                {
                    float valueRatio = (i - _minimum) / (_maximum - _minimum);
                    float yPos = tubeBottomY - (valueRatio * tubePixelHeight);

                    // 绘制刻度线
                    g.DrawLine(tickPen, tubeRect.Right, yPos, tubeRect.Right + 10, yPos);

                    // 绘制标签文本
                    string text = i.ToString();
                    SizeF textSize = g.MeasureString(text, textFont);
                    g.DrawString(text, textFont, textBrush, tubeRect.Right + 12, yPos - textSize.Height / 2);
                }
            }
        }
    }
}